// 引入express服务器
const express = require('express');
const pool = require('../pool');
// 创建路由器对象
const about = express.Router();
// 1.新增相关
// -------------------1.1 新增评论信息------------------------
about.post("/ac", (req, res, next) => {
  var obj = req.body;
  console.log(obj);
     let sql = "INSERT INTO user_comment (user_id,goods_id,comment) VALUES (?,?,?)";
     pool.query(sql, [obj.user_id,obj.goods_id,obj.comment], (err, result) => {
        if (err) {
            next(err);
            return;
          }
        res.send({
          code: 200,
          msg:'添加评论成功'
        })
    })
})
// -------------------1.2 新增问题信息------------------------
about.post("/aq", (req, res, next) => {
  var obj = req.body;
  console.log(obj);
     let sql = "INSERT INTO user_question (user_id,goods_id,question) VALUES (?,?,?)";
     pool.query(sql, [obj.user_id,obj.goods_id,obj.question], (err, result) => {
        if (err) {
            next(err);
            return;
          }
        res.send({
          code: 200,
          msg:'添加问题成功'
        })
    })
})
// -------------------1.3 新增回答信息------------------------
about.post("/aa", (req, res, next) => {
  var obj = req.body;
  console.log(obj);
     let sql = "INSERT INTO user_answer (user_id,ques_id,goods_id,answer) VALUES (?,?,?,?)";
     pool.query(sql, [obj.user_id,obj.ques_id,obj.goods_id,obj.answer], (err, result) => {
        if (err) {
            next(err);
            return;
          }
        res.send({
          code: 200,
          msg:'添加回答成功'
        })
    })
})
// 2.查询
// ---------------5.查询信息------------------
about.get("/details", (req, res, next) => {
  let obj = req.query;
  console.log(obj);
  let sql = 'SELECT comment,question,answer FROM (user_comment INNER JOIN user_question ON user_comment.goods_id=user_question.goods_id) INNER JOIN user_answer ON user_question.qid=user_answer.ques_id WHERE user_comment.goods_id=?';
  pool.query(sql,[obj.goods_id], (err, result) => {
    if (err) {
      next(err);
      return;
    }
    if (result.length == 0) {
      res.send({
        code: 201,
        msg: '查询失败',
      });
    } else {
      res.send({
        code: 200,
        msg: '查询成功',
        data: result,
      });
    }
  })
})
// --------------存取图片信息--------------
about.post("/pic", (req, res, next) => {
  var obj = req.body;
  console.log(obj);
     let sql = "INSERT INTO lpk_avatar (pic) VALUES (?)";
     pool.query(sql, [obj.avatar], (err, result) => {
        if (err) {
            next(err);
            return;
          }
        res.send({
          code: 200,
          msg:'添加成功'
        })
    })
})
// --------------存取图片信息--------------
about.post("/pic", (req, res, next) => {
  var obj = req.body;
  console.log(obj);
     let sql = "INSERT INTO lpk_avatar (pic) VALUES (?)";
     pool.query(sql, [obj.avatar], (err, result) => {
        if (err) {
            next(err);
            return;
          }
        res.send({
          code: 200,
          msg:'添加成功'
        })
    })
})
module.exports = about;